<template>
  <el-drawer v-model="drawerVisible" :destroy-on-close="true" size="450px" :title="`${drawerProps.title}账号`">
    <el-form
      ref="ruleFormRef"
      label-width="100px"
      label-suffix=" :"
      :rules="rules"
      :disabled="drawerProps.isView"
      :model="drawerProps.rowData"
      :hide-required-asterisk="drawerProps.isView"
    >
      <el-form-item label="账户名" prop="username">
        <el-input v-model="drawerProps.rowData.username" placeholder="请输入账号名称" clearable></el-input>
      </el-form-item>
      <el-form-item label="手机" prop="phone">
        <el-input v-model="drawerProps.rowData.phone" placeholder="请输入手机号" clearable></el-input>
      </el-form-item>
      <el-form-item label="所属部门" prop="dept_id">
        <el-tree-select
            v-model="drawerProps.rowData.dept_id"
            :data="drawerProps.deptTree"
            :props="{ label: 'name', value: 'id' }"
            :default-expanded-keys="[drawerProps.rowData.dept_id]"
            :check-strictly="true"
            value-key="id"
            node-key="id"
            filterable
            clearable
        />
      </el-form-item>
      <el-form-item label="角色" prop="role_id">
        <el-select v-model="drawerProps.rowData.role_id" placeholder="角色" clearable>
          <el-option v-for="item in drawerProps.roleList" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="头像" prop="avatar">
        <UploadImg v-model:imageUrl="drawerProps.rowData.avatar" width="80px" height="80px" :file-size="3">
          <template #empty>
            <el-icon><Avatar /></el-icon>
            <span>请上传头像</span>
          </template>
          <template #tip> 头像大小不能超过 3M </template>
        </UploadImg>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-radio-group v-model="drawerProps.rowData.status">
          <el-radio :value="1">启用</el-radio>
          <el-radio :value="0">禁用</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="drawerVisible = false"> 取消 </el-button>
      <el-button v-show="!drawerProps.isView" type="primary" @click="handleSubmit"> 确定 </el-button>
    </template>
  </el-drawer>
</template>

<script setup lang="ts" name="UserDrawer">
import { ref, reactive } from "vue";
import {ElMessage, ElTreeSelect, FormInstance} from "element-plus";
import UploadImg from "@/components/Upload/Img.vue";

const rules = reactive({
  username: [{ required: true, message: "请输入账号名称" }],
  phone: [{ required: true, message: "请输入手机号" }],
  dept_id: [{ required: true, message: "请选择部门" }],
  role_id: [{ required: true, message: "请选择角色" }]
});

interface DrawerProps {
  title: string;
  isView: boolean;
  rowData: Partial<any>;
  deptTree: any[];
  roleList: any[];
  api?: (params: any) => Promise<any>;
  getTableList?: () => void;
}

const drawerVisible = ref(false);
const drawerProps = ref<DrawerProps>({
  isView: false,
  title: "",
  rowData: {},
  deptTree: [],
  roleList: []
});

// 接收父组件传过来的参数
const acceptParams = (params: DrawerProps) => {
  drawerProps.value = params;
  drawerVisible.value = true;
};

// 提交数据（新增/编辑）
const ruleFormRef = ref<FormInstance>();
const handleSubmit = () => {
  ruleFormRef.value!.validate(async valid => {
    if (!valid) return;
    try {
      await drawerProps.value.api!(drawerProps.value.rowData);
      ElMessage.success({ message: `${drawerProps.value.title}账号成功！` });
      drawerProps.value.getTableList!();
      drawerVisible.value = false;
    } catch (error) {
      console.log(error);
    }
  });
};

defineExpose({
  acceptParams
});
</script>
